import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:osc/models/entity/osc_author.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/button/pd_button.dart';
import 'package:osc/utils/string_util.dart';

import '../small_widgets.dart';
import 'osc_portraint_widget.dart';

class OscAuthorWidget extends StatelessWidget {
  final OscAuthor author;
  final String date;
  final bool showRelation;
  final bool showAuthorTag;
  final Function callback;

  const OscAuthorWidget({Key key, @required this.author, @required this.date, this.showRelation = false, this.showAuthorTag = false, this.callback}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    int id = 0;
    int relation = -1;
    String portrait = "";
    String name = "";
    bool officialMember = false;
    if(author != null){
      id = author.id;
      relation = author.relation;
      name = author.name;
      portrait = author.portrait;
      officialMember = author.identity.officialMember;
    }
    return Row(
      children: <Widget>[
        OscPortraitWidget(id: id, portraitImageUrl: portrait,),
        Expanded(
          child: Padding(
            padding: EdgeInsets.only(left: Dimens.gap_view_biggest),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                    children:[
                      _buildText(name, Colors.black, Dimens.font_sp14),
                      Offstage(
                        offstage: !showAuthorTag,
                        child: Container(
                          margin: EdgeInsets.only(left: Dimens.gap_view_normal),
                          padding: EdgeInsets.symmetric(horizontal: Dimens.gap_view_smaller, vertical: Dimens.gap_view_smallest),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(Dimens.view_radius_size_smaller),
                            border: Border.all(
                              color: Colors.green,
                            ),
                          ),
                          child: _buildText(officialMember?"官方人员":"吃瓜群众", Colors.green, Dimens.font_sp10),
                        ),
                      ),
                    ]
                ),
                SmallWidgets.buildLine(color: Colors.transparent, height: Dimens.gap_view_smaller),
                _buildText(StringUtil.isEmpty(date)?"":TimelineUtil.formatByDateTime(DateTime.parse(date), dayFormat: DayFormat.Full), Colors.black54, Dimens.font_sp10),
              ],
            ),
          ),
        ),
        Offstage(
          offstage: !showRelation,
          child: FocusButton(data: relation == 1 || relation == 2? "已关注": "关注", onTap: callback,),
        )
      ],
    );
  }

  _buildText(String data, Color color, double fontSize) {
    return SmallWidgets.buildText(data, color, fontSize);
  }
}
